package com.shwread.android.qysw.activity.pay;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.NewActionHelp;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw.activity.BaseSimpleActivity;
import com.shwread.android.qysw.bean.PartyFeeBean;
import com.shwread.android.qysw.bean.PartyFeeDetail;
import com.shwread.android.qysw10000229.R;
import com.shwread.android.ui.widget.refresh.PullToRefreshScrollView;
import com.shwread.android.ui.widget.refresh.PullToRefreshUtil;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by lexy on 2016/12/9.
 * 缴纳党费
 */

public class PartyFeePayListActivity extends BaseSimpleActivity {

    private ListView lvPartyFeePay;
    private TextView tvFeePayEmptyView;
    private FeePayAdapter feePayAdapter = null;
    private PullToRefreshScrollView refreshView;

    //标题
    private Button btnBack;
    private TextView tvTitle;
    private int pageNum = 1;
    private int pageSize = 100;
    private int totalPage = 0;
    private String symbol = "￥";

    private List<PartyFeeDetail> orderList = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_party_fee_pay_list);

//        refreshView = (PullToRefreshScrollView) findViewById(R.id.refreshView);
        PullToRefreshUtil.initLabel(this, refreshView);
        lvPartyFeePay = (ListView) findViewById(R.id.lvPartyFeePay);
        btnBack = (Button) findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.VISIBLE);
        tvTitle = (TextView) findViewById(R.id.common_header_name);
        tvFeePayEmptyView = (TextView) findViewById(R.id.tvFeePayEmptyView);
        tvFeePayEmptyView.setVisibility(View.GONE);
        tvTitle.setText("党费缴纳");
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PartyFeePayListActivity.this.finish();
            }
        });
        lvPartyFeePay.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(PartyFeePayListActivity.this, PartyFeePayActivity.class);
//                intent.putExtra("feedata",orderList.get(position));
                intent.putExtra("statu", orderList.get(position).getStatus());
                intent.putExtra("orderId", orderList.get(position).getId());
                startActivity(intent);
            }
        });
        orderList = new ArrayList<>();

//        refreshView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
//            @Override
//            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
//                if (refreshView.isHeaderShown()) {//
//
//                } else if (refreshView.isFooterShown()) {
//
//                }
//                refreshView.onRefreshComplete();
//            }
//        });
//        refreshView.setVisibility(View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        NewActionHelp.qryPartyFeeOrderList(this, pageNum, pageSize, new QryPartyFeeOrderListHandler());
    }

    private class FeePayAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return orderList.size();
        }

        @Override
        public Object getItem(int position) {
            return orderList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ViewHolder h;
            if (view == null) {
                h = new ViewHolder();
                view = LayoutInflater.from(PartyFeePayListActivity.this).inflate(R.layout.item_lv_party_fee_pay, null);
                h.tvTotalFee = (TextView) view.findViewById(R.id.tvTotalFee);
                h.tvPayStatu = (TextView) view.findViewById(R.id.tvPayStatu);
                h.tvPayDate = (TextView) view.findViewById(R.id.tvPayDate);
                h.tvPartyFeeMonth = (TextView) view.findViewById(R.id.tvPartyFeeMonth);
                view.setTag(h);
            } else {
                h = (ViewHolder) view.getTag();
            }

            DecimalFormat df = new DecimalFormat("0.00");
            //1没交，2已缴
            if (orderList.get(position).getStatus() == 1) {
                h.tvPayStatu.setText("待缴纳");
                h.tvPayStatu.setTextColor(Color.parseColor("#e38e2d"));
                h.tvTotalFee.setText(String.valueOf(symbol) + df.format(orderList.get(position).getPartyFees() / 100));
            } else {
                h.tvPayStatu.setText("已缴纳");
                h.tvPayStatu.setTextColor(Color.parseColor("#a5a5a5"));
                h.tvTotalFee.setText(String.valueOf(symbol) + df.format(orderList.get(position).getActualPartyFees() / 100));
            }
            h.tvPayDate.setText(orderList.get(position).getPayTime());
            h.tvPartyFeeMonth.setText(orderList.get(position).getPartyfeesMonth() + "党费");
            return view;
        }

        class ViewHolder {
            TextView tvTotalFee;
            TextView tvPayStatu;
            TextView tvPayDate;
            TextView tvPartyFeeMonth;
        }
    }

    class QryPartyFeeOrderListHandler extends ObjectHttpResponseHandler<PartyFeeBean> {

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            Log.e("党费订单列表---Failure", responseString);
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, PartyFeeBean partyFeeBean, String responseString) {
            Log.e("党费订单列表---Success", responseString);
            orderList = partyFeeBean.getOrderList();
            totalPage = partyFeeBean.getPage().getTotalPage();
            if (orderList == null || orderList.size() == 0) {
                lvPartyFeePay.setEmptyView(tvFeePayEmptyView);
            } else {
                feePayAdapter = new FeePayAdapter();
                lvPartyFeePay.setAdapter(feePayAdapter);
            }
        }

        @Override
        public Type getType() {
            return new TypeToken<PartyFeeBean>() {
            }.getType();
        }
    }
}
